package com.logicwin.RobotTravel.Services.Impl;

import com.logicwin.RobotTravel.R;
import com.logicwin.RobotTravel.Position.Position;
import com.logicwin.RobotTravel.Services.IPositionService;

public class PositionService extends Service implements IPositionService{
	private Position [] mPositions;
	private Position mCurPosition;
	private int mSize;
	
	private final int mWidth = 48;
	private final int mHeight = 48; //标示图标大小
	
	private final int mControlWidth = 64;
	private final int mControlHeight = 64;
	private final int mControlX = 10;
	private final int mControlY = 406;
	
	private final int mUndoX = 760;
	private final int mUndoY = 386;
	private boolean mPlay = false;
	
	public PositionService(){
		mPositions = new Position[3];
		mSize	= 3;
		mPositions[0] = new Position();
		mPositions[0].setScreenX(372); //大会堂
		mPositions[0].setScreenY(12);
		
		mPositions[0].setText("省人民大会堂");
		mPositions[0].setAudio("/sdcard/1.mp3");
		mPositions[0].setAudioId(R.raw.sound1);
		
		mPositions[1] = new Position();
		mPositions[1].setScreenX(135);
		mPositions[1].setScreenY(266);
		mPositions[1].setText("白堤");
		
		mPositions[1].setAudio("/sdcard/2.mp3");
		mPositions[1].setAudioId(R.raw.sound2);
		
		mPositions[2] = new Position();
		mPositions[2].setScreenX(595);
		mPositions[2].setScreenY(394);		
		mPositions[2].setText("河坊街");
		mPositions[2].setAudio("/sdcard/3.mp3");
		mPositions[2].setAudioId(R.raw.sound3);
		
/*		mPositions[0].setMacAddress("94:0c:6d:d3:f4:38");
		mPositions[1].setMacAddress("94:0c:6d:70:13:98");
		mPositions[2].setMacAddress("00:23:cd:28:a4:02");*/
//		移动mac地址
		mPositions[0].setMacAddress("00:60:b3:c9:a7:40");
		mPositions[1].setMacAddress("00:11:b5:17:67:20");
		mPositions[2].setMacAddress("00:11:b5:17:65:41");
			
		//mCurPosition = mPositions[0];
	}
	public Position getCurPosition(){
		return mCurPosition;
	}
	public int getCurPositionId(){
		if(mCurPosition == mPositions[0]) return 1;
		else if(mCurPosition == mPositions[1]) return 2;
		else return 3;
	}
	public void setCurrPosition(Position position){
		mCurPosition = position;
	}
	public void setCurPosition(Position position){
		mCurPosition = position;
	}
	public 	Position getPostion(int idx){
		if(idx <0 || idx >= mSize)
			return null;
		return mPositions[idx];
	}
	
	public int getPositions(){
		return mSize;
	}
	public boolean start() {
		return true;
	}
	
	public boolean hitControl(int x, int y){
		if( x >= mControlX && x<=  mControlX + mControlWidth
				&& y >= mControlY && y <= mControlY + mControlHeight)
			return true;
		return false;
	}
	
	public boolean hitCurrPosition(int x, int y){
		return check(x,y, mCurPosition);
	}
	
	public int hitPosition(int x, int y){
		for(int i=0; i<mSize; i++){
			if( check(x,y, i))
				return i;
		}
		return -1;
	}
	private boolean check(int x, int y, Position position){
		if(position == null) return false;
		if( x >= position.getScreenX() && x<=  position.getScreenX() + mWidth
				&& y >= position.getScreenY() && y <= position.getScreenY() + mHeight)
			return true;
		return false;
	}
	
	private boolean check(int x, int y, int idx){
		if(idx <0 || idx >= mSize) return false;
		if( x >= mPositions[idx].getScreenX() && x<=  mPositions[idx].getScreenX() + mWidth
				&& y >= mPositions[idx].getScreenY() && y <= mPositions[idx].getScreenY() + mHeight)
			return true;
		return false;
	}
	
	public boolean stop() {
		return true;
	}	
	public Position findPosition(String macAddress){
		for(int i=0; i<3; i++){
			if(mPositions[i].getMacAddress().equals(macAddress))
				return mPositions[i];
		}
		return null;	
/*		if(mCurPosition == mPositions[0]) return mPositions[1];
		else return mPositions[0];*/
		/*
		for(int i=0; i<mSize; i++){
			if(macAddress.equals(mPositions[i].getMacAddress())) return mPositions[i];
		}
		*/
		//return null;
	}
	public boolean getPlay(){
		return mPlay;
	}
	public void setPlay(boolean playStatus){
		mPlay = playStatus;
	}
	public int getControlX(){
		return mControlX;
	}
	public int getControlY(){
		return mControlY;
	}
	
	public int getUndoX(){
		return mUndoX;
	}

	public int getUndoY(){
		return mUndoY;
	}
	public boolean hitUndo(int x, int y){		
		if( x >= mUndoX && x<=  mUndoX + mControlWidth
				&& y >= mUndoY && y <= mUndoY + mControlHeight)
			return true;
		return false;		
				
	}
	public boolean checkMacAddr(String strMac){
		for(int i=0; i<3; i++){
			if(mPositions[i].getMacAddress().equals(strMac))
				return true;
		}
		return false;
	}
}
